﻿using OF.BLL.Dish;
using OF.Controls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using OF.Common;
namespace OF.Web.admin
{
    public partial class orderlist : ShopAdminPage
    {
        BLL.Order.Order bll_order = new BLL.Order.Order();
        BLL.User.Seller bll_seller = new BLL.User.Seller();
        string strWhere = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string state = OF.Common.OFRequest.GetQueryString("state");
                if (!string.IsNullOrWhiteSpace(state))
                {
                    strWhere = " state=" + state;
                    ddlState.SelectedValue = state;
                }
                Bind();
            }
        }

        public void Bind()
        {
            if (string.IsNullOrWhiteSpace(strWhere))
                strWhere = " sellerid =" + WebUtils.GetId()+" and state!=6";
            else
                strWhere += " and sellerid =" + WebUtils.GetId() + " and state!=6";
            OFPager.RecordCount = bll_order.GetRecordCount(strWhere);
            rptList.DataSource = bll_order.GetPageList(strWhere, "", OFPager.CurrentPageIndex, OFPager.PageSize);
            rptList.DataBind();

            if (rptList.Items.Count == 0)
                Nodata.Visible = true;
            else
                Nodata.Visible = false;
        }

        protected void Pager_PageChanged(object sender, EventArgs e)
        {
            Bind();
        }

        public string GetBuyerName(object buyerid)
        {
            string cartname = string.Empty;
            if (buyerid != null)
            {
                Model.Buyer model = bll_seller.GetBuyerModel(Convert.ToInt32(buyerid));
                if (model != null)
                    return model.loginname;
            }
            return string.Empty;
        }

        public string GetState(object state, object id)
        {
            int temp = Convert.ToInt32(state);
            string str = " <td><a href='orderdetail.aspx?orderid=" + id + "'>";
            string result = "";
            if (temp == (int)Enmus.ORDER_STATE.INIT)
                result = "<font color='red'>待处理</font>" + str + "处理";
            else if (temp == (int)Enmus.ORDER_STATE.CONFIRM_SUC)
                result = "处理中" + str + "查看";
            else if (temp == (int)Enmus.ORDER_STATE.CONFIRM_Fail)
                result = "不处理" + str + "查看";
            else if (temp == (int)Enmus.ORDER_STATE.COMPLETE)
                result = "已完成" + str + "查看";
            else if (temp == (int)Enmus.ORDER_STATE.CANCEL)
                result = "已取消" + str + "查看";
            else if (temp == (int)Enmus.ORDER_STATE.INVALID)
                result = "<font color='blue'>已失效</font>" + str + "查看";
            return result + "</a></td>";
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            string sql = "";
            if (!string.IsNullOrWhiteSpace(txtkey.Text.Trim()))
            {
                sql = " (ordersn like '%" + txtkey.Text.Trim() + "%' or phone like '%" + txtkey.Text.Trim() + "%') ";
            }
            var sc = new SearcherColletion(this);
            strWhere = sc.GetSqlWhere("basic");
            if (!string.IsNullOrWhiteSpace(sql))
            {
                if (string.IsNullOrWhiteSpace(strWhere))
                    strWhere = sql;
                else
                    strWhere += " and " + sql;
            }
            OFPager.SearchCriteria = strWhere;
            Bind();
        }
    }
}